package routines;

import java.io.File;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
	private static Long openedSessions = 0l;
	private static Session badsession = null;
	private static SessionFactory sessionFactory;
	private static File cfgfile = null;
	private static java.util.Properties props;
	public static Configuration configuration =  new Configuration();

	static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			// loads configuration and mappings
			if (cfgfile == null)
				configuration.configure();
			else
				configuration.configure(cfgfile);
			
//			pgDWH_Database=AsIs
//			pgDWH_Login=dataintegrator
//			pgDWH_Password=3bd6a0e8ae8b9b759959406ad10636e4
//			pgDWH_Port=54321
//			pgDWH_Schema=public
//			pgDWH_Server=localhost
//			configuration.setProperty("hibernate.connection.url", value);
			
			
			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
					.applySettings(configuration.getProperties()).build();

			// builds a session factory from the service registry
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}

		return sessionFactory;
	}
	
	public static void InitBadSession(){
		if(badsession==null)
			badsession=openSession();
	}

	public static void closeSessionFactory() {
		if(null!=badsession)
			closeSession(badsession);

		if (sessionFactory!=null)
			sessionFactory.close();
		sessionFactory = null;

	}

	public static void printOpenedSessions() {
		System.out.println("Number of opend sessions is "
				+ openedSessions.toString());
	}

	public static Session openSession() {
		openedSessions++;
		return getSessionFactory().openSession();

	}

	public static void closeSession(Session s) {
		if (s != null) {
			openedSessions--;
			s.close();
		}

	}

	public static void delete(Object o) {
		Session s = openSession();
		s.beginTransaction();
		s.delete(o);
		s.getTransaction().commit();
		closeSession(s);
	}

	public static Object Save(Object o) {
		Session s = openSession();
		s.beginTransaction();
		s.saveOrUpdate(o);
		s.getTransaction().commit();
		closeSession(s);
		return o;
	}

	public static int executeUpdate(String q) {
		Session s = openSession();
		s.beginTransaction();
		int res = s.createQuery(q).executeUpdate();
		s.getTransaction().commit();
		closeSession(s);
		return res;
	}

	public static int executeSQLUpdate(String q) {
		Session s = openSession();
		s.beginTransaction();
		int res = s.createSQLQuery(q).executeUpdate();
		s.getTransaction().commit();
		closeSession(s);
		return res;
	}

	public static java.math.BigInteger getBadINN2() {
		String q = "select nextval('seq_bad_inn')";
		Object ob = badsession.createSQLQuery(q).uniqueResult();
		return (java.math.BigInteger) ob;
	}

	public static java.math.BigInteger getBadINN() {
		String q = "select nextval('seq_bad_inn')";
		Session s = openSession();
		Object ob = s.createSQLQuery(q).uniqueResult();
		closeSession(s);
		return (java.math.BigInteger) ob;
	}

	public static List execute(String q) {

		Session s = openSession();
		// s.beginTransaction();
		List l = s.createQuery(q).list();
		closeSession(s);
		return l;
	}

	private static int currentsave = 0;
	private static int batchSize = 100000;
	private static Session batchSession = null;

	public static void BatchSave(Object o) {
		if (batchSession == null) {
			batchSession = HibernateUtil.getSessionFactory().openSession();
			batchSession.beginTransaction();
		}
		batchSession.save(o);
		if (currentsave++ % batchSize == 0) {
			batchSession.flush();
			batchSession.clear();
			BatchSaveCommit();
		}
	}

	public static void BatchSaveCommit() {
		if (batchSession == null)
			return;
		batchSession.getTransaction().commit();
		batchSession.close();
		batchSession = null;
	}

	public static void setCfgfile(String cfgfile) {
		HibernateUtil.cfgfile = new File(cfgfile);
	}

}